package com.ruoyi.service;

import com.ruoyi.entity.BizParkingSpace;
import com.ruoyi.entity.BizPriceParkingSpace;
import com.ruoyi.mapper.BizPriceParkingSpaceMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.bean.BizPriceParkingSpaceFrom;
import com.sxftech.resp.ResponseData;

import java.util.List;

/**
 * <p>
 * 价格体系与车位关系表 服务实现类
 * </p>
 *
 * @author liuxing
 * @since 2020-10-20
 */
@Service
public class BizPriceParkingSpaceService extends ServiceImpl<BizPriceParkingSpaceMapper, BizPriceParkingSpace> {

	public ResponseData create(BizPriceParkingSpaceFrom bizPriceParkingSpace) {
		BizPriceParkingSpace entity = transformationFrom(bizPriceParkingSpace);
		this.save(entity);
		return ResponseData.success("新增成功");
	}
	
	public ResponseData modify(BizPriceParkingSpaceFrom bizPriceParkingSpace) {
		if(findById(bizPriceParkingSpace.getId())== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizPriceParkingSpace entity = transformationFrom(bizPriceParkingSpace);
		entity.setId(bizPriceParkingSpace.getId());
		this.updateById(entity);
		return ResponseData.success("修改成功");
	}

	public BizPriceParkingSpace transformationFrom(BizPriceParkingSpaceFrom bizPriceParkingSpace) {
		BizPriceParkingSpace entity = new BizPriceParkingSpace();
		entity.setPlCode(bizPriceParkingSpace.getPlCode());
		entity.setSpaceId(bizPriceParkingSpace.getSpaceId());
		entity.setPriceSystemCode(bizPriceParkingSpace.getPriceSystemCode());
		entity.setValidateTime(bizPriceParkingSpace.getValidateTime());
		entity.setIsDeleted(bizPriceParkingSpace.getIsDeleted());
		entity.setSpaceProperty(bizPriceParkingSpace.getSpaceProperty());
		entity.setWithdrawChannel(bizPriceParkingSpace.getWithdrawChannel());
		entity.setCreator(bizPriceParkingSpace.getCreator());
		entity.setCreateTime(bizPriceParkingSpace.getCreateTime());
		entity.setModifier(bizPriceParkingSpace.getModifier());
		entity.setModifyTime(bizPriceParkingSpace.getModifyTime());
		return entity;
	}

	public BizPriceParkingSpaceFrom transformationBean(BizPriceParkingSpace bizPriceParkingSpace) {
		BizPriceParkingSpaceFrom from = new BizPriceParkingSpaceFrom();
		from.setPlCode(bizPriceParkingSpace.getPlCode());
		from.setSpaceId(bizPriceParkingSpace.getSpaceId());
		from.setPriceSystemCode(bizPriceParkingSpace.getPriceSystemCode());
		from.setValidateTime(bizPriceParkingSpace.getValidateTime());
		from.setIsDeleted(bizPriceParkingSpace.getIsDeleted());
		from.setSpaceProperty(bizPriceParkingSpace.getSpaceProperty());
		from.setWithdrawChannel(bizPriceParkingSpace.getWithdrawChannel());
		from.setCreator(bizPriceParkingSpace.getCreator());
		from.setCreateTime(bizPriceParkingSpace.getCreateTime());
		from.setModifier(bizPriceParkingSpace.getModifier());
		from.setModifyTime(bizPriceParkingSpace.getModifyTime());

		return from;
	}

	
	public BizPriceParkingSpaceFrom findById(String id) {
		BizPriceParkingSpace entity = this.getOne(new QueryWrapper<BizPriceParkingSpace>().lambda()
				.eq(BizPriceParkingSpace::getId, id)
				);
		if(entity == null) return null;
		return transformationBean(entity);
	}
	
	public ResponseData remove(String id) {
		if(findById(id)== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizPriceParkingSpace bizPriceParkingSpace = new BizPriceParkingSpace();
		bizPriceParkingSpace.setId(id);
		this.updateById(bizPriceParkingSpace);
		return ResponseData.success("删除成功");
	}
	
	public IPage pageList(Integer page, Integer size, BizPriceParkingSpaceFrom ibean) {
		return this.page(new Page(page, size), new QueryWrapper<BizPriceParkingSpace>().lambda()
				.eq(StringUtils.isNotEmpty(ibean.getPlCode()), BizPriceParkingSpace::getPlCode, ibean.getPlCode())
				.eq(ibean.getSpaceId() != null, BizPriceParkingSpace::getSpaceId, ibean.getSpaceId())
				.eq(StringUtils.isNotEmpty(ibean.getPriceSystemCode()), BizPriceParkingSpace::getPriceSystemCode, ibean.getPriceSystemCode())
				.eq(ibean.getValidateTime() != null, BizPriceParkingSpace::getValidateTime, ibean.getValidateTime())
				.eq(StringUtils.isNotEmpty(ibean.getIsDeleted()), BizPriceParkingSpace::getIsDeleted, ibean.getIsDeleted())
				.eq(StringUtils.isNotEmpty(ibean.getSpaceProperty()), BizPriceParkingSpace::getSpaceProperty, ibean.getSpaceProperty())
				.eq(StringUtils.isNotEmpty(ibean.getWithdrawChannel()), BizPriceParkingSpace::getWithdrawChannel, ibean.getWithdrawChannel())
				.eq(ibean.getCreator() != null, BizPriceParkingSpace::getCreator, ibean.getCreator())
				.eq(ibean.getCreateTime() != null, BizPriceParkingSpace::getCreateTime, ibean.getCreateTime())
				.eq(ibean.getModifier() != null, BizPriceParkingSpace::getModifier, ibean.getModifier())
				.eq(ibean.getModifyTime() != null, BizPriceParkingSpace::getModifyTime, ibean.getModifyTime())
				);
	}

	public List<BizPriceParkingSpace> oneList(String plCode, String spaceId){
		QueryWrapper<BizPriceParkingSpace> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("pl_code",plCode);
		queryWrapper.eq("space_id",spaceId);
		return list(queryWrapper);
	}
}
